package com.cn.springcloudwechat.common.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.springcloudwechat.mapper.mapper.YmUserMapper;
import com.cn.springcloudwechat.model.YmChildGrowth;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserChild;
import com.cn.springcloudwechat.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;


/**
 * 线程局部变量工具类
 *
 * @author Lg
 *
 */
public class ThreadLocalUtil {


    private static ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();

    private static ThreadLocal<HttpServletResponse> response = new ThreadLocal<HttpServletResponse>();

    public static HttpServletRequest getRequest() {
        setRequest();
        return request.get();
    }

    private static void setRequest() {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.set(req);
    }

    public static HttpServletResponse getResponse() {
        setResponse();
        return response.get();
    }

    public static void setResponse() {
        HttpServletResponse req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getResponse();
        response.set(req);
    }

    public static YmUser getTestUser(){
        YmUser ymUser = (YmUser) RedisUtil.getInstace().get("oTO_rv7uW_-vemHrL5GSwhzM_ZMA"+"user");
        return ymUser;
    }

    /**
     * 保存孩子在Redis
     * @param ymUserChild
     */
    public static void saveChild(YmUserChild ymUserChild){
        //修改Redis
        RedisUtil.getInstace().del(ymUserChild.getChildId()+"child");
        RedisUtil.getInstace().set(ymUserChild.getChildId()+"child",ymUserChild);
    }

    /**
     * 从Redis获取孩子
     * @param childId
     * @return
     */
    public static YmUserChild getYmUserChild(Long childId){
        YmUserChild ymUserChild = (YmUserChild) RedisUtil.getInstace().get(childId+"child");
        return ymUserChild;
    }

    /**
     * 获取用户孩子
     * @param userId
     * @return
     */
    public static List<YmUserChild> getUserChild(Long userId){
        List<YmUserChild> ymUserChildren = (List<YmUserChild>) RedisUtil.getInstace().get(userId+"child");
        return ymUserChildren;
    }

    /**
     * 保存用户孩子到Redis
     * @param ymUserChildren
     */
    public static void savaUserChild(List<YmUserChild> ymUserChildren){
        RedisUtil.getInstace().del(getUser().getUserId()+"child");
        RedisUtil.getInstace().set(getUser().getUserId()+"child",ymUserChildren);
    }

    public static YmUser getUser() {
        setRequest();
        YmUser ymUser = (YmUser) request.get().getSession().getAttribute("user");
        return ymUser;
    }
    public static String getRandomNumber() {
        setRequest();
        String randomNumber = (String) request.get().getSession().getAttribute("randomNumber");
        return randomNumber;
    }
    public static void setRandomNumber(String randomNumber) {
        setRequest();
        request.get().getSession().setAttribute("randomNumber",randomNumber);
    }

    public static void remove() {
        setRequest();
        request.get().getSession().removeAttribute("user");
    }
}